package daoimpl;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import pojo.Product;
import pojo.UserProfile;
import pojo.WishList;
import pojo.WishListDetail;
import dao.WishListDAO;

@Repository("WishListDAO")
public class WishListDAOImpl implements WishListDAO {
	
	private SessionFactory sessionFactory;
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public boolean addWishList(WishList wishlist, UserProfile user) {
		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
		} catch (HibernateException ex) {
			session = sessionFactory.openSession();
		}

		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			wishlist.setUserProfile(user);
			session.save(wishlist);
			transaction.commit();
		} catch (HibernateException ex) {
			transaction.rollback();
			System.err.println(ex);
			return false;
		} finally {
			session.close();
		}
		return true;
	}
	
	public boolean addProductToWishList(WishList wishlist, Product product)
	{
		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
		} catch (HibernateException ex) {
			session = sessionFactory.openSession();
		}

		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			WishListDetail wishListDetail = new WishListDetail();
			wishListDetail.setProduct(product);
			wishListDetail.setWishList(wishlist);
			session.save(wishListDetail);
			transaction.commit();
		} catch (HibernateException ex) {
			transaction.rollback();
			System.err.println(ex);
			return false;
		} finally {
			session.close();
		}
		return true;
	}

	public WishList getWishListByEmail(String email) {
		WishList wl = null;

		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
		} catch (HibernateException ex) {
			session = sessionFactory.openSession();
		}

		try {
			String hql = "Select wl from WishList wl where wl.userProfile.email=:email ";
			Query query = session.createQuery(hql);
			query.setParameter("email", email);
			wl = (WishList) query.uniqueResult();
		} catch (HibernateException ex) {
			System.err.println(ex);
		} finally {
			session.close();
		}
		return wl;
	}

	public boolean deleteProductInWishList(WishList wishlist, Product product) {
		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
		} catch (HibernateException ex) {
			session = sessionFactory.openSession();
		}

		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			
			String hql = "delete from WishListDetail wd where wd.wishList.id=:wishlishid and wd.product.sku=:sku";
			Query query = session.createQuery(hql);
			query.setParameter("wishlishid", wishlist.getId());
			query.setParameter("sku", product.getSku());
			query.executeUpdate();
			
			
			transaction.commit();
		} catch (HibernateException ex) {
			transaction.rollback();
			System.err.println(ex);
			return false;
		} finally {
			session.close();
		}
		return true;
	}
}
